package com.yanqu.road.dao.impl.activity.dragonboatrace;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dragonboatrace.DragonBoatRaceUnionData;
import com.yanqu.road.entity.activity.dragonboatrace.DragonBoatRaceVoyageRecord;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/5/14 18:14
 */
public class DragonBoatRaceUnionDataDaoImpl extends TempDataObjectDao<DragonBoatRaceUnionData> {

    public static String getDragonBoatRaceUnionDataSql = "select * from `t_c_activity_dragonboatrace_union_data` where `activityId`=? ";
    public Map<String,DragonBoatRaceUnionData> getDragonBoatRaceUnionDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String,DragonBoatRaceUnionData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDragonBoatRaceUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DragonBoatRaceUnionData dragonBoatRaceUnionData = parseData(rs);
                map.put(dragonBoatRaceUnionData.getUnionId(),dragonBoatRaceUnionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDragonBoatRaceUnionDataSql = "update `t_c_activity_dragonboatrace_union_data` set distance=?,recordRefreshTime=?,recordList=?,receivedInfoMap=?,memberRecordMap=?,unionBaseInfo=? where activityId=? and unionId=?";
    @Override
    protected boolean updateData(DragonBoatRaceUnionData dragonBoatRaceUnionData) {
        dragonBoatRaceUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dragonBoatRaceUnionData.getDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUnionData.getRecordRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getReceivedInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getMemberRecordMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dragonBoatRaceUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updateDragonBoatRaceUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDragonBoatRaceUnionDataSql = "insert into `t_c_activity_dragonboatrace_union_data`(activityId,unionId,distance,recordRefreshTime,recordList,receivedInfoMap,memberRecordMap,unionBaseInfo) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DragonBoatRaceUnionData dragonBoatRaceUnionData) {
        dragonBoatRaceUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dragonBoatRaceUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, dragonBoatRaceUnionData.getDistance()));
        para.put(i++, new DbParameter(Types.INTEGER, dragonBoatRaceUnionData.getRecordRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getRecordList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getReceivedInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getMemberRecordMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dragonBoatRaceUnionData.getUnionBaseInfo())));
        if (getDb().executeNoneQuery(insertDragonBoatRaceUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DragonBoatRaceUnionData parseData(ResultSet rs) throws SQLException {
        DragonBoatRaceUnionData dragonBoatRaceUnionData = new DragonBoatRaceUnionData();
        dragonBoatRaceUnionData.setActivityId(rs.getInt("activityId"));
        dragonBoatRaceUnionData.setUnionId(rs.getString("unionId"));
        dragonBoatRaceUnionData.setDistance(rs.getLong("distance"));
        dragonBoatRaceUnionData.setRecordRefreshTime(rs.getInt("recordRefreshTime"));
        dragonBoatRaceUnionData.setRecordList(JSON.parseObject(rs.getString("recordList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.dragonboatrace.DragonBoatRaceVoyageRecord>>() {}));
        dragonBoatRaceUnionData.setReceivedInfoMap(JSON.parseObject(rs.getString("receivedInfoMap"), new TypeReference<java.util.Map<java.lang.Integer, java.util.List<java.lang.Long>>>() {}));
        dragonBoatRaceUnionData.setMemberRecordMap(JSON.parseObject(rs.getString("memberRecordMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.dragonboatrace.DragonBoatRaceMemberRecord>>() {}));
        dragonBoatRaceUnionData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        for(DragonBoatRaceVoyageRecord item : dragonBoatRaceUnionData.getRecordList()){
            dragonBoatRaceUnionData.getRecordMap().put(item.getRoleId(),item);
        }
        return dragonBoatRaceUnionData;
    }



}
